package com.smartodo.utils;
/**
 * @author Anirban Maiti
 */
import java.util.*;

class HtmlSelectOption
{
	public String value = "";
	public String text = "";
	public String title = "";
	public String otherAttr = "";
	public boolean selected = false;
}

public class HtmlSelectBox 
{
	private String id;
	private String title = "";
	private String css_class = "GeneralSelectBox";
	private String otherAttr = null;
	private ArrayList<HtmlSelectOption> optionList = null;
	
	
	public HtmlSelectBox(String id, String title, String otherAttr)
	{
		this.id = id;
		this.title = title;
		this.otherAttr = otherAttr;
		optionList = new ArrayList<HtmlSelectOption>();
	}
	
	public void addOption(String value, String text)
	{
		HtmlSelectOption option = new HtmlSelectOption();
		option.value = value;
		option.text = text;
		optionList.add(option);
	}
	
	public void setSelected(String value)
	{
		for (int i = 0; i < optionList.size(); i++) {
			HtmlSelectOption option = optionList.get(i);
			if(option.value.compareTo(value) == 0)
			{
				option.selected = true;
				return;
			}
		}
	}
	
	public String Generate()
	{
		String out = "";
		out = "<select id='"+this.id+"' name='"+this.id+"' class='"+this.css_class+"' size='1' ";
		if(this.otherAttr != null)
			out += this.otherAttr + " ";
		out += ">";
		

		for (int i = 0; i < optionList.size(); i++) 
		{
			HtmlSelectOption option = optionList.get(i);
			out += "<option value='"+option.value+"' ";
			if(option.otherAttr != null && option.otherAttr.length() != 0)
				out += " "+option.otherAttr+" ";
			if(option.selected == true)
				out += " selected ";
			out += ">" + option.text + "</option>";
		}
		out += "</select>";
		return out;
	}
}
